package com.weibo.favorite.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.weibo.common.CommonUtil;
import com.weibo.common.DB;
import com.weibo.favorite.Favorite;
import com.weibo.favorite.dao.FavoriteDao;
import com.weibo.sql.CommonParm;
import com.weibo.sql.SqlFile;

public class FavoriteDaoImp implements FavoriteDao {
	private static Logger logger =  Logger.getLogger(FavoriteDaoImp.class);
	@Override
	public String addFavorite(Favorite favorite) {
		// TODO Auto-generated method stub
		String flag = "";
		ResultSet rs = DB.plsqlExec_res(CommonUtil.getSqlBy_par(SqlFile.FAVORITE_UNIQUE, favorite.getUser_id(),favorite.getHost_id()));
		try {
			if(!rs.next()){
				DB.plsqlExec(SqlFile.FAVORITE_ADD, favorite.getUser_id(),
							favorite.getHost_id(),
							favorite.getFav_other());
				flag = CommonParm.FAVORITE_ADD_SUCCESS;
			}else{
				flag  = CommonParm.FAVORITE_ALREADY_EXIST;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e);
			flag = CommonParm.FAVORITE_ADD_FAILE;
		}
		return flag;
	}

	@Override
	public boolean deleteFavorite(String fav_id) {
		// TODO Auto-generated method stub
		return DB.plsqlExec(SqlFile.FAVORITE_DELETE, fav_id);
	}

	@Override
	public boolean executeSql(String sql) {
		// TODO Auto-generated method stub
		return DB.sqlNonExeQuery(sql);
	}

	@Override
	public List getFavoriteList(int currentPage,String user_id) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(CommonUtil.getSqlBy_par(SqlFile.FAVORITE_SELECT, user_id), currentPage));
	}

	@Override
	public List getFavoriteList_bySql(String sql) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(sql);
	}

}
